package core;

import fileoperation.FileDeletion;
import repository.Repository;

import java.io.File;
import java.io.IOException;
import gitobject.Index;

public class JitAdd {
    /**
     * Add files to index.
     * @param file
     * @throws Exception
     */
    public static void add(File file) throws Exception {
        if(!file.exists()) {
            throw new IOException("File doesn't exist.");
        }


        File indexFile = new File(Repository.getGitDir() + File.separator + "index");
        Index index;
        if (indexFile.exists()) {
            index = Index.deserialize(indexFile);
            index.addFile(file);
            FileDeletion.deleteFile(indexFile);
            index.writeObject();
        } else {
            index = new Index();
            index.addFile(file);
            index.writeObject();
        }
    }
}
